package org.tigris.subversion.svnant;

import java.io.File;
import java.util.TimeZone;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.ProjectComponent;
import org.apache.tools.ant.types.Reference;
import org.tigris.subversion.svnant.commands.SvnCommand;
import org.tigris.subversion.svnant.types.SvnSetting;
import org.tigris.subversion.svnclientadapter.ISVNClientAdapter;
import org.tigris.subversion.svnclientadapter.ISVNPromptUserPassword;
import org.tigris.subversion.svnclientadapter.SVNClientAdapterFactory;
import org.tigris.subversion.svnclientadapter.SVNClientException;
import org.tigris.subversion.svnclientadapter.commandline.CmdLineClientAdapterFactory;
import org.tigris.subversion.svnclientadapter.javahl.JhlClientAdapterFactory;
import org.tigris.subversion.svnclientadapter.svnkit.SvnKitClientAdapterFactory;

public class SvnFacade
{
  private static final String MSG_MISSING_DEPENDENCY = "Missing '%s' dependencies on the classpath !";
  private static final String DEFAULT_DATEFORMATTER = "MM/dd/yyyy hh:mm a";
  private static final Boolean DEFAULT_SVNKIT = Boolean.FALSE;
  private static final Boolean DEFAULT_JAVAHL = Boolean.TRUE;
  private static final Boolean DEFAULT_FAILONERROR = Boolean.TRUE;
  private static final String KEY_FACADE = "org.tigris.subversion.svnant.SvnFacade";
  private static Boolean javahlAvailable = null;
  private static Boolean svnKitAvailable = null;
  private static Boolean commandLineAvailable = null;
  private SvnSetting setting = new SvnSetting(null);
  private SvnSetting refidsetting = null;
  private Reference refid = null;

  private static final SvnFacade getFacade(ProjectComponent paramProjectComponent)
  {
    if ((paramProjectComponent instanceof SvnCommand))
      paramProjectComponent = ((SvnCommand)paramProjectComponent).getTask();
    String str = "org.tigris.subversion.svnant.SvnFacade" + paramProjectComponent.hashCode();
    SvnFacade localSvnFacade = (SvnFacade)paramProjectComponent.getProject().getReference(str);
    if (localSvnFacade == null)
    {
      localSvnFacade = new SvnFacade();
      paramProjectComponent.getProject().addReference(str, localSvnFacade);
    }
    return localSvnFacade;
  }

  private static final SvnSetting getSetting(ProjectComponent paramProjectComponent)
  {
    return getFacade(paramProjectComponent).setting;
  }

  private static final SvnSetting getRefidSetting(ProjectComponent paramProjectComponent)
  {
    SvnFacade localSvnFacade = getFacade(paramProjectComponent);
    if (localSvnFacade.refidsetting == null)
      if (localSvnFacade.refid != null)
      {
        Object localObject = localSvnFacade.refid.getReferencedObject(paramProjectComponent.getProject());
        if (localObject == null)
          throw new BuildException("The refid attribute value '" + localSvnFacade.refid + "' doesn't refer to any object.");
        if (!(localObject instanceof SvnSetting))
          throw new BuildException("The refid attribute value '" + localSvnFacade.refid + "' has an unknown type [" + localObject.getClass().getName() + "].");
        localSvnFacade.refidsetting = ((SvnSetting)localObject);
      }
      else
      {
        localSvnFacade.refidsetting = localSvnFacade.setting;
      }
    return localSvnFacade.refidsetting;
  }

  public static final void setRefid(ProjectComponent paramProjectComponent, Reference paramReference)
  {
    getFacade(paramProjectComponent).refid = paramReference;
  }

  public static final void setJavahl(ProjectComponent paramProjectComponent, boolean paramBoolean)
  {
    getSetting(paramProjectComponent).setJavahl(paramBoolean);
  }

  public static final boolean getJavahl(ProjectComponent paramProjectComponent)
  {
    Boolean localBoolean = getSetting(paramProjectComponent).getJavahl();
    if (localBoolean == null)
      localBoolean = getRefidSetting(paramProjectComponent).getJavahl();
    if (localBoolean == null)
      localBoolean = DEFAULT_JAVAHL;
    return localBoolean.booleanValue();
  }

  public static final void setSvnKit(ProjectComponent paramProjectComponent, boolean paramBoolean)
  {
    getSetting(paramProjectComponent).setSvnKit(paramBoolean);
  }

  public static final boolean getSvnKit(ProjectComponent paramProjectComponent)
  {
    Boolean localBoolean = getSetting(paramProjectComponent).getSvnKit();
    if (localBoolean == null)
      localBoolean = getRefidSetting(paramProjectComponent).getSvnKit();
    if (localBoolean == null)
      localBoolean = DEFAULT_SVNKIT;
    return localBoolean.booleanValue();
  }

  public static final void setUsername(ProjectComponent paramProjectComponent, String paramString)
  {
    getSetting(paramProjectComponent).setUsername(paramString);
  }

  public static final String getUsername(ProjectComponent paramProjectComponent)
  {
    String str = getSetting(paramProjectComponent).getUsername();
    if (str == null)
      str = getRefidSetting(paramProjectComponent).getUsername();
    return str;
  }

  public static final void setPassword(ProjectComponent paramProjectComponent, String paramString)
  {
    getSetting(paramProjectComponent).setPassword(paramString);
  }

  public static final String getPassword(ProjectComponent paramProjectComponent)
  {
    String str = getSetting(paramProjectComponent).getPassword();
    if (str == null)
      str = getRefidSetting(paramProjectComponent).getPassword();
    return str;
  }

  public static final void setSSLPassword(ProjectComponent paramProjectComponent, String paramString)
  {
    getSetting(paramProjectComponent).setSSLPassword(paramString);
  }

  public static final String getSSLPassword(ProjectComponent paramProjectComponent)
  {
    String str = getSetting(paramProjectComponent).getSSLPassword();
    if (str == null)
      str = getRefidSetting(paramProjectComponent).getSSLPassword();
    return str;
  }

  public static final void setSSLClientCertPath(ProjectComponent paramProjectComponent, File paramFile)
  {
    getSetting(paramProjectComponent).setSSLClientCertPath(paramFile);
  }

  public static final File getSSLClientCertPath(ProjectComponent paramProjectComponent)
  {
    File localFile = getSetting(paramProjectComponent).getSSLClientCertPath();
    if (localFile == null)
      localFile = getRefidSetting(paramProjectComponent).getSSLClientCertPath();
    return localFile;
  }

  public static final void setSSHPort(ProjectComponent paramProjectComponent, Integer paramInteger)
  {
    getSetting(paramProjectComponent).setSSHPort(paramInteger);
  }

  public static final Integer getSSHPort(ProjectComponent paramProjectComponent)
  {
    Integer localInteger = getSetting(paramProjectComponent).getSSHPort();
    if (localInteger == null)
      localInteger = getRefidSetting(paramProjectComponent).getSSHPort();
    return localInteger;
  }

  public static final void setSSHPassphrase(ProjectComponent paramProjectComponent, String paramString)
  {
    getSetting(paramProjectComponent).setSSHPassphrase(paramString);
  }

  public static final String getSSHPassphrase(ProjectComponent paramProjectComponent)
  {
    String str = getSetting(paramProjectComponent).getSSHPassphrase();
    if (str == null)
      str = getRefidSetting(paramProjectComponent).getSSHPassphrase();
    return str;
  }

  public static final void setSSHKeyPath(ProjectComponent paramProjectComponent, File paramFile)
  {
    getSetting(paramProjectComponent).setSSHKeyPath(paramFile);
  }

  public static final File getSSHKeyPath(ProjectComponent paramProjectComponent)
  {
    File localFile = getSetting(paramProjectComponent).getSSHKeyPath();
    if (localFile == null)
      localFile = getRefidSetting(paramProjectComponent).getSSHKeyPath();
    return localFile;
  }

  public static final void setCertReject(ProjectComponent paramProjectComponent, Boolean paramBoolean)
  {
    getSetting(paramProjectComponent).setCertReject(paramBoolean);
  }

  public static final Boolean getCertReject(ProjectComponent paramProjectComponent)
  {
    Boolean localBoolean = getSetting(paramProjectComponent).getCertReject();
    if (localBoolean == null)
      localBoolean = getRefidSetting(paramProjectComponent).getCertReject();
    return localBoolean;
  }

  public static final void setDateFormatter(ProjectComponent paramProjectComponent, String paramString)
  {
    getSetting(paramProjectComponent).setDateFormatter(paramString);
  }

  public static final String getDateFormatter(ProjectComponent paramProjectComponent)
  {
    String str = getSetting(paramProjectComponent).getDateFormatter();
    if ((str == null) || (str.length() == 0))
      str = getRefidSetting(paramProjectComponent).getDateFormatter();
    if ((str == null) || (str.length() == 0))
      str = "MM/dd/yyyy hh:mm a";
    return str;
  }

  public static final void setDateTimezone(ProjectComponent paramProjectComponent, String paramString)
  {
    getSetting(paramProjectComponent).setDateTimezone(paramString);
  }

  public static final TimeZone getDateTimezone(ProjectComponent paramProjectComponent)
  {
    String str = getSetting(paramProjectComponent).getDateTimezone();
    if ((str == null) || (str.length() == 0))
      str = getRefidSetting(paramProjectComponent).getDateTimezone();
    if ((str == null) || (str.length() == 0))
      return null;
    return TimeZone.getTimeZone(str);
  }

  public static final void setFailonerror(ProjectComponent paramProjectComponent, boolean paramBoolean)
  {
    getSetting(paramProjectComponent).setFailonerror(paramBoolean);
  }

  public static final boolean getFailonerror(ProjectComponent paramProjectComponent)
  {
    Boolean localBoolean = getSetting(paramProjectComponent).getFailonerror();
    if (localBoolean == null)
      localBoolean = getRefidSetting(paramProjectComponent).getFailonerror();
    if (localBoolean == null)
      localBoolean = DEFAULT_FAILONERROR;
    return localBoolean.booleanValue();
  }

  private static final boolean isJavahlAvailable()
  {
    if (javahlAvailable == null)
    {
      javahlAvailable = Boolean.FALSE;
      try
      {
        JhlClientAdapterFactory.setup();
      }
      catch (SVNClientException localSVNClientException)
      {
      }
      try
      {
        if (SVNClientAdapterFactory.isSVNClientAvailable("javahl"))
          javahlAvailable = Boolean.TRUE;
      }
      catch (Exception localException)
      {
      }
    }
    return javahlAvailable.booleanValue();
  }

  private static final boolean isSVNKitAvailable()
  {
    if (svnKitAvailable == null)
    {
      svnKitAvailable = Boolean.FALSE;
      try
      {
        SvnKitClientAdapterFactory.setup();
      }
      catch (SVNClientException localSVNClientException)
      {
      }
      try
      {
        if (SVNClientAdapterFactory.isSVNClientAvailable("svnkit"))
          svnKitAvailable = Boolean.TRUE;
      }
      catch (Exception localException)
      {
      }
    }
    return svnKitAvailable.booleanValue();
  }

  private static final boolean isCommandLineAvailable()
  {
    if (commandLineAvailable == null)
    {
      commandLineAvailable = Boolean.FALSE;
      try
      {
        CmdLineClientAdapterFactory.setup();
      }
      catch (SVNClientException localSVNClientException)
      {
      }
      try
      {
        if (SVNClientAdapterFactory.isSVNClientAvailable("commandline"))
          commandLineAvailable = Boolean.TRUE;
      }
      catch (Exception localException)
      {
      }
    }
    return commandLineAvailable.booleanValue();
  }

  public static final synchronized ISVNClientAdapter getClientAdapter(ProjectComponent paramProjectComponent)
    throws BuildException
  {
    ISVNClientAdapter localISVNClientAdapter = null;
    boolean bool1 = getJavahl(paramProjectComponent);
    boolean bool2 = getSvnKit(paramProjectComponent);
    int i = 0;
    if (bool1)
      if (isJavahlAvailable())
      {
        localISVNClientAdapter = SVNClientAdapterFactory.createSVNClient("javahl");
        paramProjectComponent.log("Using javahl", 3);
      }
      else
      {
        paramProjectComponent.log(String.format("Missing '%s' dependencies on the classpath !", new Object[] { "javahl" }), 0);
      }
    if ((bool2) && (localISVNClientAdapter == null))
      if (isSVNKitAvailable())
      {
        localISVNClientAdapter = SVNClientAdapterFactory.createSVNClient("svnkit");
        paramProjectComponent.log("Using svnkit", 3);
      }
      else
      {
        paramProjectComponent.log(String.format("Missing '%s' dependencies on the classpath !", new Object[] { "svnkit" }), 0);
      }
    if ((!bool1) && (!bool2))
      if (isCommandLineAvailable())
      {
        localISVNClientAdapter = SVNClientAdapterFactory.createSVNClient("commandline");
        i = 1;
        paramProjectComponent.log("Using command line", 3);
      }
      else
      {
        paramProjectComponent.log(String.format("Missing '%s' dependencies on the classpath !", new Object[] { "commandline" }), 0);
      }
    if (localISVNClientAdapter == null)
      throw new BuildException("Cannot find javahl, svnkit nor command line svn client");
    if (getUsername(paramProjectComponent) != null)
      localISVNClientAdapter.setUsername(getUsername(paramProjectComponent));
    if (i != 0)
    {
      if (getPassword(paramProjectComponent) != null)
        localISVNClientAdapter.setPassword(getPassword(paramProjectComponent));
    }
    else
      localISVNClientAdapter.addPasswordCallback(new DefaultPasswordCallback(getUsername(paramProjectComponent), getPassword(paramProjectComponent), getSSHKeyPath(paramProjectComponent), getSSHPassphrase(paramProjectComponent), getSSHPort(paramProjectComponent), getSSLClientCertPath(paramProjectComponent), getSSLPassword(paramProjectComponent), getCertReject(paramProjectComponent)));
    return localISVNClientAdapter;
  }

  private static class DefaultPasswordCallback
    implements ISVNPromptUserPassword
  {
    private String username;
    private String password;
    private String sshprivatekeypath;
    private String sshpassphrase;
    private String sslclientcertpath;
    private String sslcertpassword;
    private Integer sshport;
    private Boolean reject;

    public DefaultPasswordCallback(String paramString1, String paramString2, File paramFile1, String paramString3, Integer paramInteger, File paramFile2, String paramString4, Boolean paramBoolean)
    {
      this.username = paramString1;
      this.password = paramString2;
      this.sshprivatekeypath = (paramFile1 != null ? paramFile1.getAbsolutePath() : null);
      this.sshpassphrase = paramString3;
      this.sshport = paramInteger;
      this.sslclientcertpath = (paramFile2 != null ? paramFile2.getAbsolutePath() : null);
      this.sslcertpassword = paramString4;
      this.reject = paramBoolean;
    }

    public boolean promptSSL(String paramString, boolean paramBoolean)
    {
      return (this.sslclientcertpath != null) || (this.sslcertpassword != null);
    }

    public String getSSLClientCertPassword()
    {
      return this.sslclientcertpath;
    }

    public String getSSLClientCertPath()
    {
      return this.sslcertpassword;
    }

    public int askTrustSSLServer(String paramString, boolean paramBoolean)
    {
      if (Boolean.TRUE.equals(this.reject))
        return 0;
      return 1;
    }

    public String getSSHPrivateKeyPath()
    {
      return this.sshprivatekeypath;
    }

    public String getSSHPrivateKeyPassphrase()
    {
      return this.sshpassphrase;
    }

    public boolean promptSSH(String paramString1, String paramString2, int paramInt, boolean paramBoolean)
    {
      if (paramInt > 0)
        this.sshport = Integer.valueOf(paramInt);
      return (this.sshpassphrase != null) || (this.sshprivatekeypath != null);
    }

    public int getSSHPort()
    {
      if (this.sshport == null)
        return 22;
      return this.sshport.intValue();
    }

    public boolean userAllowedSave()
    {
      return false;
    }

    public String getUsername()
    {
      return this.username;
    }

    public String getPassword()
    {
      return this.password;
    }

    public String askQuestion(String paramString1, String paramString2, boolean paramBoolean1, boolean paramBoolean2)
    {
      return "";
    }

    public boolean prompt(String paramString1, String paramString2, boolean paramBoolean)
    {
      return true;
    }

    public boolean promptUser(String paramString1, String paramString2, boolean paramBoolean)
    {
      return true;
    }

    public boolean askYesNo(String paramString1, String paramString2, boolean paramBoolean)
    {
      return paramBoolean;
    }
  }
}

/* Location:           C:\Users\Administrator\Desktop\svnant.jar
 * Qualified Name:     org.tigris.subversion.svnant.SvnFacade
 * JD-Core Version:    0.6.0
 */